<template>
	<div>
		<el-input v-model="username" placeholder="请输入账号"></el-input>
		<el-input v-model="password" placeholder="请输入密码"></el-input>
		<el-button type="primary" @click="login">登录</el-button>
	</div>
</template>

<script>
// import axios from 'axios'
export default {
	data() {
		return {
			username: '',
			password: '',
		}
	},
	methods: {
		login() {
			this.$http
				.post('/user/signin', {
					username: this.username,
					password: this.password,
				})
				.then((res) => {
					// res = res.data
					if (res.errcode == 0) {
						// console.log(res.data)

						//通过commit调用mutations里面的setUser来修改state数据
						this.$store.commit('setUser', res.data)
						localStorage.setItem('token', res.data.token)

						this.$http.get('/user/info').then((res) => {
							// res = res.data
							if (res.errcode == 0) {
								this.$store.commit('setUser', res.data)
							} else {
								this.$router.push('/login')
							}
						})

						this.$router.push('/') //跳转页面

						// this.$store.state.user = res.data
					} else {
						this.$message.error(res.errmsg)
					}
					// console.log(res)
				})
		},
	},
}
</script>

<style lang=" "></style>
